/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.miscellaneous.test;

import java.util.Properties;
import java.util.Set;

import phoside.PhosphoProteins;
import phoside.PhosphoProteins.PhosphoType;

import phoside.ui.task.AbstractTask;

/**
 *
 */
public class PhosideTestTask extends AbstractTask {
    private PhosphoProteins phosphoData;
    private PhosphoProteins negValidData;
    private Properties props;
    private Set<PhosphoType> types;
    private int kfold;
    private int repeat;
    private boolean negTestSameSizeOrPercentage;
    private int negSize;

    private final CrossValidationTest tester;

    private final LeaveOneOutTest looTester;

    public PhosideTestTask(final PhosphoProteins phosphoData,
                            final PhosphoProteins negValidData,
                            final Set<PhosphoType> types,
                            final Properties props,
                            final int negSize) {
            super("Testing");
            this.phosphoData = phosphoData;
            this.negValidData = negValidData;
            this.props = props;
            this.types = types;
            this.negSize = negSize;
            looTester = new LeaveOneOutTest();
            tester = null;
    }

    public PhosideTestTask(final PhosphoProteins phosphoData,
                            final Set<PhosphoType> types,
                            final Properties props,
                            final int kfold,
                            final int repeat,
                            final boolean negTestSameSizeOrPercentage) {
            super("Testing");
            this.phosphoData = phosphoData;
            this.props = props;
            this.types = types;
            this.kfold = kfold;
            this.repeat = repeat;
            this.negTestSameSizeOrPercentage = negTestSameSizeOrPercentage;
            tester = new CrossValidationTest();
            looTester = null;
    }

    /**
     * Executes Task.
     */
    //@Override
    public void run() {
            try {
                    taskMonitor.setPercentCompleted(-1);
                    
                    taskMonitor.setStatus("Test...");
                    if (tester!=null) {
                        tester.setTaskMonitor(taskMonitor);
                        obj = tester.test(phosphoData, types, props, kfold, repeat, negTestSameSizeOrPercentage);
                    } else {
                        looTester.setTaskMonitor(taskMonitor);
                        obj = looTester.test(phosphoData, negValidData, types, props, negSize);
                    }
                    taskMonitor.setPercentCompleted(100);
                    taskMonitor.setStatus("Testing result has been saved.");

                    success = true;
            } catch (Exception e) {
                    taskMonitor.setPercentCompleted(100);
                    taskMonitor.setStatus("Failed to train model.\n"+e.getMessage());
                    e.printStackTrace();
                    return;
            }
    }
}
